package com.example.demo.Mapper;

import com.example.demo.Entity.Education;
import com.example.demo.Entity.PersonalInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface EducationMapper {
    @Select("SELECT * FROM education WHERE person_id = #{userId}")
    List<Education> findByUserId(Long userId);

    // 批量更新教育背景
    void batchUpdateEducations(@Param("educations") List<Education> educations);

    @Update("<script>" +
            "<foreach collection='list' item='edu' separator=';'>" +
            "UPDATE education SET " +
            "person_id = #{edu.person_id}, " +
            "school_name = #{edu.school_name}, " +
            "degree = #{edu.degree}, " +
            "major = #{edu.major}, " +
            "start_date = #{edu.start_date}, " +
            "end_date = #{edu.end_date}, " +
            "rank_info = #{edu.rank_info} " +
            "WHERE id = #{edu.id} AND person_id = #{userId}" +  // 添加安全校验
            "</foreach>" +
            "</script>")
    void update(@Param("userId") Long userId,
                     @Param("list") List<Education> educations);

    // 删除指定用户的所有教育背景
    @Delete("DELETE FROM education WHERE person_id = #{userId}")
    int deleteByUserId(Long userId);

    // 删除单个教育背景
    @Delete("DELETE FROM education WHERE id = #{id}")
    int deleteById(Long id);

    // 插入教育背景
    @Insert("INSERT INTO education(person_id, school_name, degree, major, start_date, end_date, rank_info) " +
            "VALUES(#{person_id}, #{school_name}, #{degree}, #{major}, #{start_date}, #{end_date}, #{rank_info})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Education education);

}
